/**
 *
 */
package com.wedroid.r2d2.support.sync;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.alibaba.fastjson.annotation.JSONField;

/**
 * @author lute
 *
 */
public class SyncKey {

    @JSONField(name = "Count")
    private int count;

    @JSONField(name = "List")
    private List<SyncKeyEntry> list;

    /**
     * Default constructor
     */
    public SyncKey() {

    }

    /**
     * Get sync key concatenated string
     * @return
     */
    @JSONField(serialize = false)
    public String getSyncKeyString() {
        StringBuilder builder = new StringBuilder();
        for (SyncKeyEntry entry : list) {
            builder.append(entry.getKey()).append("_").append(entry.getVal()).append("|");
        }
        if (builder.length() > 0) {
            return builder.substring(0, builder.length() - 1);
        } else {
            return "";
        }
    }

    /**
     * @return
     */
    @JSONField(serialize = false)
    public Map<String, Object> getSyncKey() {
        Map<String, Object> syncKey = new HashMap<>();
        syncKey.put("Count", this.count);

        List<Map<String, Integer>> entryList =
                this.list.stream()
                    .map(entry -> {
                        Map<String, Integer> syncKeyEntry = new HashMap<>();
                        syncKeyEntry.put("Key", entry.getKey());
                        syncKeyEntry.put("Val", entry.getVal());
                        return syncKeyEntry;
                    })
                    .collect(Collectors.toList());

        syncKey.put("List", entryList);
        return syncKey;
    }

    /**
     * @return the count
     */
    public int getCount() {
        return count;
    }

    /**
     * @return the list
     */
    public List<SyncKeyEntry> getList() {
        return list;
    }

    /**
     * @param count the count to set
     */
    public void setCount(int count) {
        this.count = count;
    }

    /**
     * @param list the list to set
     */
    public void setList(List<SyncKeyEntry> list) {
        this.list = list;
    }

}
